﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.SystemCenter.Orchestrator.Integration;
using LinqToTwitter;
using SocialMediaIntegrationPack.Properties;

namespace SocialMediaIntegrationPack
{
    [Activity("Tweet!")]
    public class TweetAct : TwitterActivity
    {

        [ActivityInput("Text")]
        public string TweetText { get; set; }

        [ActivityInput("MediaFileName")]
        public string MediaFileName { get; set; }


        public TweetAct()
        {
        }

        [ActivityMethod]
        public void Execute()
        {
            try
            {
                if (String.IsNullOrEmpty(TweetText))
                {
                    throw new ArgumentException(Resources.StatusMinLengthError);
                }
                if (TweetText.Length > STATUS_MAX_LENGTH)
                {
                    throw new ArgumentException(Resources.StatusMaxLengthError);
                }

                var twitterContext = Authorize();

                // Posting Tweet
                var media = LoadMedia(MediaFileName);
                var status = media != null 
                    ? twitterContext.TweetWithMedia(TweetText, true, new List<Media> { media }) 
                    : twitterContext.UpdateStatus(TweetText);                

                Response = string.Format(Resources.TweetAct_Run_Status_ID, status.StatusID);
            }
            catch (Exception exception)
            {
                LogException(exception);
                throw;
            }
        }

        /// <summary>
        /// Load media from file to twitter format
        /// </summary>
        private Media LoadMedia(string mediaFileName)
        {
            if (!MediaFound(mediaFileName)) {
                return null;
            }

            return new Media {
                                FileName = Path.GetFileName(mediaFileName),
                                ContentType = GetContentTypeByExtension(mediaFileName),
                                Data = File.ReadAllBytes(mediaFileName)
                            };
        }

        /// <summary>
        /// Return MediaContentType by file extension
        ///     PNG - default Media Content
        /// </summary>
        private MediaContentType GetContentTypeByExtension(string fileName)
        {
            
            if (string.IsNullOrEmpty(fileName)) {
                throw new ArgumentException();
            }

            var ext = Path.GetExtension(fileName);
            if (ext==null)
                return  MediaContentType.Png;
            ext = ext.ToLower();
            if (ext == "jpg") {
                return MediaContentType.Jpeg;
            } 

            if (ext == "gif"){
                return MediaContentType.Gif;
            }

            return MediaContentType.Png;
        }

        /// <summary>
        /// Checks that media exists
        /// </summary>
        private bool MediaFound(string mediaFileName)
        {
            return !string.IsNullOrEmpty(mediaFileName) && File.Exists(mediaFileName);
        }
    }
}
